package com.yhq.basic.algorithm.map;

import java.util.HashMap;
import java.util.Iterator;

/**
 * @author hqyin
 * @date 2025/5/21 3:39 PM
 */
public class TestHashMap {
    public static void main(String[] args) {
        HashMap<Integer,Integer> map = new HashMap<>(10);
        map.put(1,1);
        map.put(2,2);
        map.put(3,3);


    //modCount 这是fail-fast机制
        //抛 java.util.HashMap$HashIterator
//        for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
//            map.remove(entry.getKey());
//        }

        //遍历删除使用Iterator的remove
        Iterator<Integer> it = map.keySet().iterator();
        while (it.hasNext()) {
//           map.remove( it.next()); //抛 java.util.HashMap$HashIterator
            //解决方案，使用 it.remove
            it.next();
            it.remove();
        }


    }
}
